// pages/cart/cart.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      getIndex:-1,
      total_price:'0.00',
      count:0,
      all_choose:false,
      cart:[],
  },
    // 跳转到商城页
  toMarkert: function () {
    wx.navigateTo({
      url: '/pages/product/product',
    })
  },
  // 获取formid
  getFormId: function (e) {
    var formid = e.detail.formId;
    console.log(formid);
    wx.request({
      url: app.siteInfo.siteroot + 'saveformid',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        formid: formid
      },
      success: function (res) {

      }
    })
  },
  //获取外层下标
  getIndex:function(e){
      var index=e.currentTarget.id;
      this.setData({
          getIndex:index,
      })

  },
  //选中
    choosegoods:function(){
        var that=this;
        var getIndex=that.data.getIndex;
        var cart=that.data.cart;
        if(cart[getIndex].ischoose){
            cart[getIndex].ischoose=false;
        }else{
            cart[getIndex].ischoose = true;
        }
        var arr=[];
        cart.forEach((item)=>{
            if(item.ischoose){
                arr.push(item.ischoose);
            }
        })
        if(arr.length==cart.length){
            that.setData({
                all_choose:true,
            })
        }else{
            that.setData({
                all_choose:false,
            })
        }
        that.setData({
            cart:cart,
            count:arr.length,

        })
        this.cac();
       
    },
    //全部选中//全部取消
    allchoose:function(){
        var that=this;
        var cart=that.data.cart;
        var all_choose=that.data.all_choose;
        if(all_choose==false){
            all_choose =true;
            cart.forEach(function(item) {
                item.ischoose=true;
            })
            that.setData({
                count:cart.length,
            })
           
        }else{
            all_choose=false;
            cart.forEach(function(item) {
                item.ischoose = false;
            })
            that.setData({
                count: 0,
            })
        }
        that.setData({
            all_choose: all_choose,
            cart:cart,
        })
        this.cac();
    },
    //   减少
    dec: function () {
        var that = this;
        var getIndex=that.data.getIndex;
        var cart = that.data.cart;
        var num = parseInt(cart[getIndex].num);
      var goodsid = parseInt(cart[getIndex].goodsid);
        var id = parseInt(cart[getIndex].id);
        num--;
      cart[getIndex].num = num;
        if (cart[getIndex].num < 1) {
            cart[getIndex].num = 1;
            num = 1;
            wx.showToast({
                title: '不能再减少了',
                image: '/img/fail.png',
                duration: 1500,
            })
        }else{
          wx.request({
            url: app.siteInfo.shoppath + 'cargoodsnum',
            method: "post",
            data: {
              uid: app.siteInfo.uid,
              pid: app.siteInfo.pid,
              com_part_id: app.siteInfo.com_part_id,
              num: num,
              id: id,
              goodsid: goodsid,
            },
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  cart: cart,
                })
                that.cac();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  image: '/img/fail.png',
                  duration: 1500,
                })
              }
            }
          })
        }
        
        
    },
    // 增加
    add: function () {
        var that = this;
        var cart = that.data.cart;
        var getIndex=that.data.getIndex;
        var id = parseInt(cart[getIndex].id);
      var goodsid = parseInt(cart[getIndex].goodsid);
        var num = parseInt(cart[getIndex].num);
        num++;
        cart[getIndex].num = num;
      wx.request({
        url: app.siteInfo.shoppath + 'cargoodsnum',
        method: "post",
        data: {
          uid: app.siteInfo.uid,
          pid: app.siteInfo.pid,
          com_part_id: app.siteInfo.com_part_id,
          num: num,
          goodsid: goodsid,
          id: id,
        },
        success: function (res) {
          if(res.data.code == 200){
            that.setData({
              cart: cart,
            })
            that.cac();
          }else{
            wx.showToast({
              title: res.data.msg,
              image: '/img/fail.png',
              duration: 1500,
            })
          }
          
        }
      })
    },
    //删除
    delete: function () {
        var that = this;
        var getIndex=that.data.getIndex;
        var cart=that.data.cart;
        wx.showModal({
            title: '',
            content: '确定要删除吗?',
            showCancel: true,
            success: function (res) {
                if (res.cancel) {
                } else {
                    var id = cart[getIndex].id;
                    console.log(id)//删除商品的id
                    wx.request({
                      url: app.siteInfo.shoppath+'cargoodsdel',
                      method:"post",
                      data:{
                        uid: app.siteInfo.uid,
                        pid: app.siteInfo.pid,
                        com_part_id: app.siteInfo.com_part_id,
                        id: id,
                      },
                      success:function(res){
                        if(res.data.code == 200){
                          cart.splice(getIndex, 1);
                          var arr = [];
                          cart.forEach(item => {
                            if (item.ischoose) {
                              arr.push(item.ischoose);
                            }
                          })

                          that.setData({
                            cart: cart,
                            count: arr.length,
                          })
                          that.cac();
                        }else{
                          wx.showToast({
                            title: res.data.msg,
                            image: '/img/fail.png',
                            duration: 1500,
                          })
                        }
                      }
                    })
                    
                }
             }
        })
       
       
    },
    //结算
    balance:function(){
        var that=this;
        var cart=that.data.cart;
        var arr = [];
        cart.forEach((item) => {
            if (item.ischoose) {
                arr.push(item.goodsid)
            }
        })
        console.log(arr)//所有选中商品的id
        if(arr.length==0){
            wx.showToast({
                title: '请选择商品',
                image: '/img/fail.png',
                duration: 1500,
            })
        }else{
          var ids = arr.join(',');
            wx.navigateTo({
              url: '/pages/order_sure/order_sure?source=shopcar&ids='+ids,
            })
        }
    },
    //计算总价格
    cac:function(){
        var that=this;
        var cart=that.data.cart;
        var priceList=[];
        cart.forEach(item=>{
            if(item.ischoose){
              var price1 = parseFloat(item.price) * item.num;
                priceList.push(price1)
            }
        })
        console.log(priceList)
        var total_price=0;
        if (priceList.length > 0){
            priceList.forEach(item => {
                total_price += item;
            })
        }
        that.setData({
            total_price: total_price.toFixed(2),
        })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.siteInfo.shoppath+'getshopcar',
      method:"post",
      data:{
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        com_part_id: app.siteInfo.com_part_id,
      },
      success:function(res){
        that.setData({
          cart: res.data.list,
          all_choose:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})